package com.sntk.registermanager.common;

import cn.hutool.core.date.DateException;
import com.sntk.registermanager.common.exception.DefinedException;
import com.sntk.registermanager.common.infrastructure.resp.ResponseData;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: GlobalExceptionHandler
 * @author: wgchao
 * @createTime: 2019/9/3 11:12 AM
 * @Description: 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(DateException.class)
    public ResponseData exceptionHandler(HttpServletRequest request, DateException exception) throws Exception {
        return handleErrorInfo(request, exception.getMessage(), "199", exception);
    }

    @ExceptionHandler(DefinedException.class)
    public ResponseData exceptionHandler(HttpServletRequest request, DefinedException exception) throws Exception {
        return handleErrorInfo(request, exception.getMessage(), exception.getCode(), exception);
    }

    @ExceptionHandler(Exception.class)
    public ResponseData exceptionHandler(HttpServletRequest request, Exception exception) throws Exception {
        return handleErrorInfo(request, exception.getMessage(), "999", exception);
    }

    private ResponseData<String> handleErrorInfo(HttpServletRequest request, String message, String code, Exception exception) {
        exception.printStackTrace();
        return ResponseData.error(code, message);
    }



}
